package com.qxzn.module.health.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qxzn.module.health.R;

import java.util.List;

public class HealthItemAdapter extends RecyclerView.Adapter<HealthItemAdapter.ViewHolder> {
    List<HealthItem> dataList;

    public HealthItemAdapter(List<HealthItem> itemList) {
        dataList = itemList;
    }

    @NonNull
    @Override
    public HealthItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.health_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HealthItem healthItem = dataList.get(position);
        holder.icon.setImageResource(healthItem.iconRes);
        holder.name.setText(healthItem.name);
        holder.content.setText(Html.fromHtml(healthItem.html));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name;
        TextView content;

        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_health_icon);
            name = itemView.findViewById(R.id.tv_health_name);
            content = itemView.findViewById(R.id.tv_health_content);
        }
    }

    public static class HealthItem {
        int iconRes;
        String name;
        SpannableString spannableString;
        String patternHtml;
        String html = "--";

        public HealthItem(int iconRes, String name) {
            this.iconRes = iconRes;
            this.name = name;
        }
    }
}
